<template>
  <common-layout>
    <div class="login_box">
      <div class="login_left">
        <img src="../../assets/img/loginlogo.png" alt="">
      </div>
      <div class="login_right">
        <div class="top">
          <div class="header">
            <!-- <img alt="logo" class="logo" src="@/assets/img/logo.png" /> -->
            <span class="title">{{ systemName }}</span>
          </div>
          <!-- <div class="desc">abp vNext(net5) + ant vue admin</div> -->
        </div>
        <div class="login">
          <a-form @submit="onSubmit" :form="form">
            <!-- <a-tabs size="large" :tabBarStyle="{textAlign: 'center'}" style="padding: 0 2px;">
              <a-tab-pane tab="账户密码登录" key="1"> -->
            <a-alert
              type="error"
              :closable="true"
              v-show="error"
              :message="error"
              showIcon
              style="margin-bottom: 24px"
            />
            <a-form-item>
              <a-input
                size="large"
                addon-before="租户"
                disabled
                placeholder="未选择"
                v-model="tenant"
              >
                <a-button slot="suffix" type="primary" @click="visible = true">
                  切换
                </a-button>
              </a-input>
              <!-- <a-input-search
                placeholder="租户未选择"
              
                size="large"
              >
                <a-button slot="enterButton" @click="test"> 切换 </a-button>
              </a-input-search> -->
              <!-- <a-input
                autocomplete="autocomplete"
                size="large"
                placeholder="租户"
                v-decorator="['tenant']"
              >
                <a-icon slot="prefix" type="user" />
              </a-input> -->
            </a-form-item>
            <a-form-item>
              <a-input
                autocomplete="autocomplete"
                size="large"
                placeholder="admin"
                v-decorator="[
                  'name',
                  {
                    rules: [
                      { required: true, message: '请输入账户名', whitespace: true },
                    ],
                  },
                ]"
              >
                <a-icon slot="prefix" type="user" />
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-input
                size="large"
                placeholder="1q2w3E*"
                autocomplete="autocomplete"
                type="password"
                v-decorator="[
                  'password',
                  {
                    rules: [
                      { required: true, message: '请输入密码', whitespace: true },
                    ],
                  },
                ]"
              >
                <a-icon slot="prefix" type="lock" />
              </a-input>
            </a-form-item>
            <!-- </a-tab-pane> -->
            <!-- <a-tab-pane tab="手机号登录" key="2">
                <a-form-item>
                  <a-input size="large" placeholder="mobile number">
                    <a-icon slot="prefix" type="mobile" />
                  </a-input>
                </a-form-item>
                <a-form-item>
                  <a-row :gutter="8" style="margin: 0 -4px">
                    <a-col :span="16">
                      <a-input size="large" placeholder="captcha">
                        <a-icon slot="prefix" type="mail" />
                      </a-input>
                    </a-col>
                    <a-col :span="8" style="padding-left: 4px">
                      <a-button style="width: 100%" class="captcha-button" size="large">获取验证码</a-button>
                    </a-col>
                  </a-row>
                </a-form-item>
              </a-tab-pane> -->
            <!-- </a-tabs> -->
            <div>
              <a-checkbox :checked="true">自动登录</a-checkbox>
              <!-- <a style="float: right">忘记密码</a> -->
            </div>
            <a-form-item>
              <a-button
                :loading="logging"
                style="width: 100%; margin-top: 24px;"
                size="large"
                htmlType="submit"
                type="primary"
                class="login_btn"
                >登录</a-button
              >
            </a-form-item>
            <div>
              其他登录方式
              <a-icon class="icon" type="wechat"  @click="$refs.weChatLoginModal.createOrEdit()" />
              <!-- <a-icon class="icon" type="alipay-circle" />
              <a-icon class="icon" type="taobao-circle" />
              <a-icon class="icon" type="weibo-circle" />
              <router-link style="float: right" to="/dashboard/workplace">注册账户</router-link> -->
            </div>
          </a-form>
          <a-modal
            title="切换租户"
            :visible="visible"
            :confirm-loading="confirmLoading"
            @ok="tenantSwitchModal"
            @cancel="visible = false"
          >
            <a-input v-model="tenantName" size="large" placeholder="租户名称" />
          </a-modal>
        </div>
        <page-footer class="login-footer" :link-list="footerLinks" :copyright="copyright"></page-footer>
        <we-chat-login-modal ref="weChatLoginModal" @ok="handleOk($event)"  />
      </div>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from "@/layouts/CommonLayout";
import PageFooter from '@/layouts/footer/PageFooter'
import {
  login
} from "@/services/user";
import { setAuthorization } from "@/utils/request";
import { mapMutations } from "vuex";
import { tenantSwitch } from "@/services/multiTenancy/tenant";
import WeChatLoginModal from "./modules/WeChatLoginModal";
import {mapState} from 'vuex'
export default {
  name: "Login",
  components: { CommonLayout, WeChatLoginModal, PageFooter },
  data() {
    return {
      logging: false,
      error: "",
      form: this.$form.createForm(this),
      visible: false,
      confirmLoading: false,
      tenant: "",
      tenantName: "",
    };
  },
  computed: {
    ...mapState('setting', ['footerLinks', 'copyright']),
    systemName() {
      return this.$store.state.setting.systemName;
    },
  },
  methods: {
    ...mapMutations("account", ["setUser", "setPermissions", "setRoles"]),
    onSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err) => {
        if (!err) {
          this.logging = true;
          const name = this.form.getFieldValue("name");
          const password = this.form.getFieldValue("password");
          const tenant = this.tenant;
          login(name, password, tenant)
            .then(this.afterLogin)
            .finally(() => {
              this.logging = false;
            });
        }
      });
    },
    afterLogin(res) {
      this.logging = false;
      const loginRes = res;
      if (loginRes) {
        // const { user, permissions, roles } = loginRes.data;
        // this.setUser(user);
        // this.setPermissions(permissions);
        // this.setRoles(roles);
        setAuthorization({
          token: loginRes.access_token,
          expireAt: new Date(new Date().getTime() + loginRes.expires_in),
        });
        // 获取路由配置
        // getRoutesConfig().then((result) => {
        //   const routesConfig = result.data.data;
        //   loadRoutes(
        //     { router: this.$router, store: this.$store, i18n: this.$i18n },
        //     routesConfig
        //   );
        //   this.$router.push("/demo");
        //   this.$message.success(loginRes.message, 3);
        // });
        this.$store.dispatch('account/refreshPermissions', res => {
          if (res == 'success') {
            this.$message.success("登录成功", 3);
            this.$router.push('/demo/dashboard/analysis')
          }
        })
      } else {
        this.error = loginRes.message;
      }
    },
    handlePermissions(obj) {
      let permissions = [];
      if (!obj) {
        return permissions;
      }
      permissions = Object.keys(obj).map((x) => {
        return {
          id: x,
          operation: [],
        };
      });
      return permissions;
      // let list = Object.keys(obj).map((x) => {
      //   let n = x.split(".").length - 1;
      //   return {
      //     val: x,
      //     num: n,
      //   };
      // });
      // let idList = list.filter((x) => x.num == 1);
      // permissions = idList.map((x) => {
      //   let operation = list
      //     .filter((y) => y.num == 2 && y.val.indexOf(x.val) > -1)
      //     .map((y) => {
      //       return y.val.split(".")[2];
      //     });
      //   return {
      //     id: x.val,
      //     operation: operation,
      //   };
      // });
      // return permissions;
    },
    tenantSwitchModal() {
      this.confirmLoading = true;
      let params = {
        name: this.tenantName,
      };
      tenantSwitch(params)
        .then((res) => {
          this.visible = false;
          this.tenant = this.tenantName;
        })
        .finally(() => {
          this.confirmLoading = false;
        });
    },
    handleOk(res){
      this.afterLogin(res);
    }
  },
};
</script>

<style lang="less" scoped>
.common-layout {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    display: flex;
    justify-content: center;
    align-items: center;
  .login_box{
    background-color: transparent;
    width: 1080px;
    height: 800px;
    top: 50%;
    transform: translate(0,2px);
    .login_left{
      width: 50%;
      height: 100%;
      float: left;
      background: #f90;
    }
    .login_right{
      float: left;
      width: 50%;
      height: 100%;
      background: #fff;
      padding: 160px 75px 0;
      position: relative;
      .login-footer {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
      }
    }
  }
  .top {
    text-align: center;
    margin-bottom: 30px;
    .header {
      height: 44px;
      line-height: 44px;
      a {
        text-decoration: none;
      }
      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }
      .title {
        font-size: 30px;
        color: @title-color;
        font-family: "Myriad Pro", "Helvetica Neue", Arial, Helvetica,
          sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
        
      }
    }
    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }
  .login {
    width: 368px;
    margin: 0 auto;
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }
    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
  }
}
@media (max-width:1200px){
    .common-layout{
      .login_box{
        width:100%;
        height:600px;
        .login_left{display: none;}
        .login_right{
          width:100%;float: none;
          padding: 60px 20px 0;
          .title{font-size:20px;}
        }
      }
      
    }

}
</style>
